<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的座位订单ID" prop="seatOrderId">
        <el-input
          v-model="queryParams.seatOrderId"
          placeholder="请输入关联的座位订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-input
          v-model="queryParams.paymentMethod"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker clearable
          v-model="queryParams.paymentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="关联的座位订单ID" align="center" prop="seatOrderId" />
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="订单状态：1待支付，2已支付，3已取消，4已完成" align="center" prop="status" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="关联的座位订单ID" prop="seatOrderId">
          <el-input v-model="form.seatOrderId" placeholder="请输入关联的座位订单ID" />
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable
            v-model="form.orderTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">菜品订单详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDishorderitem">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDishorderitem">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="dishorderitemList" :row-class-name="rowDishorderitemIndex" @selection-change="handleDishorderitemSelectionChange" ref="dishorderitem">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="菜品ID" prop="dishId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dishId" placeholder="请输入菜品ID" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="quantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.quantity" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="price" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入单价" />
            </template>
          </el-table-column>
          <el-table-column label="小计" prop="subtotal" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.subtotal" placeholder="请输入小计" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDishorder, getDishorder, delDishorder, addDishorder, updateDishorder } from "@/api/Dishorder.js"

export default {
  name: "Dishorder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDishorderitem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品订单管理表格数据
      dishorderList: [],
      // 菜品订单详情表格数据
      dishorderitemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        seatOrderId: null,
        orderTime: null,
        status: null,
        totalPrice: null,
        paymentMethod: null,
        paymentTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询菜品订单管理列表 */
    getList() {
      this.loading = true
      listDishorder(this.queryParams).then(response => {
        this.dishorderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        seatOrderId: null,
        orderTime: null,
        status: null,
        totalPrice: null,
        paymentMethod: null,
        paymentTime: null,
        remark: null
      }
      this.dishorderitemList = []
      this.$refs.form?.resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm?.resetFields();
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加菜品订单管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDishorder(id).then(response => {
        this.form = response.data
        this.dishorderitemList = response.data.dishorderitemList
        this.open = true
        this.title = "修改菜品订单管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dishorderitemList = this.dishorderitemList
          if (this.form.id != null) {
            updateDishorder(this.form).then(response => {
              if (response.code === 200){
                    this.$message.success({
                    message: '修改成功',
                    duration: 1000 // 自定义展示时间，单位：毫秒
                    });
                    this.open = false
                    this.getList()
                }else {
                    this.$message.error({
                    message: '修改失败'+response.msg,
                    duration: 1000
                    });
                }
            })
          } else {
            addDishorder(this.form).then(response => {
              if (response.code === 200){
                    this.$message.success({
                    message: '新增成功',
                    duration: 1000 // 自定义展示时间，单位：毫秒
                    });
                    this.open = false
                    this.getList()
                }else {
                    this.$message.error({
                    message: '新增失败：'+response.msg,
                    duration: 1000
                    });
                }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除评价编号为"' + ids + '"的数据项？', '确认', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            return delDishorder(ids)
        }).then(() => {
            this.getList()
            this.$message.success('删除成功')
        }).catch(() => {
            // 取消删除时的回调，可选
            this.$message.info('已取消删除')
        })
    },
	/** 菜品订单详情序号 */
    rowDishorderitemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 菜品订单详情添加按钮操作 */
    handleAddDishorderitem() {
      let obj = {}
      obj.dishId = ""
      obj.quantity = ""
      obj.price = ""
      obj.subtotal = ""
      this.dishorderitemList.push(obj)
    },
    /** 菜品订单详情删除按钮操作 */
    handleDeleteDishorderitem() {
      if (this.checkedDishorderitem.length == 0) {
        this.$modal.msgError("请先选择要删除的菜品订单详情数据")
      } else {
        const dishorderitemList = this.dishorderitemList
        const checkedDishorderitem = this.checkedDishorderitem
        this.dishorderitemList = dishorderitemList.filter(function(item) {
          return checkedDishorderitem.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleDishorderitemSelectionChange(selection) {
      this.checkedDishorderitem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dishorder/export', {
        ...this.queryParams
      }, `dishorder_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
